package de.mfits.fireflyplayer;

import static de.mfits.fireflyplayer.storage.DbHelper.ALBUM;
import static de.mfits.fireflyplayer.storage.DbHelper.ARTIST;
import static de.mfits.fireflyplayer.storage.DbHelper.ID;
import static de.mfits.fireflyplayer.storage.DbHelper.SONGS;
import static de.mfits.fireflyplayer.storage.DbHelper.TITLE;
import static de.mfits.fireflyplayer.storage.DbHelper.TRACK_NUMBER;
import android.content.Intent;
import android.os.Bundle;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import de.mfits.fireflyplayer.service.PlaybackService;

public class BrowseSongsActivity extends BrowseActivity {
  private static final String TABLE = SONGS;
  private static final String[] COLUMNS = { ID, ARTIST, TITLE, ALBUM, TRACK_NUMBER };
  private static final String SELECTION = null;
  private static final String GROUP_BY = null;
  private static final String HAVING = null;
  private static final String ORDER_BY = null;

  private static final int LAYOUT = R.layout.list_songs;
  private static final String[] FROM = { ARTIST, TITLE, ALBUM, TRACK_NUMBER };
  private static final int[] TO = { R.id.songArtist, R.id.songTitle, R.id.songAlbum,
      R.id.trackNumber };

  private PlaybackService player;

  public BrowseSongsActivity() {
    this(SELECTION);
  }

  public BrowseSongsActivity(String selection) {
    super(TABLE, COLUMNS, selection, GROUP_BY, HAVING, ORDER_BY, LAYOUT, FROM, TO);
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    player = PlaybackService.bindClient(this);
    setTitle(R.string.browse_title_songs);
  }

  @Override
  protected void onListItemClick(ListView l, View v, int position, long id) {
    try {
      player.play((int) id);
      startActivity(new Intent(this, PlaybackActivity.class));
    } catch (RemoteException e) {
      Log.e(getClass().getSimpleName(), e.getMessage(), e);
    }
  }
}
